//document.ready = function() {
  $(document).ready(function () {


	//-------------------------KNAPPEN I NÄRHETEN----------------------------------------------
	// Add a click listener on the button to get the location data
	$('#getWiki').click(function() {
		if(navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(onSuccess, onError);
			//console.log(onSuccess);

		} else {
			// If location is not supported on this platform, disable it
			$('#getWiki').value = "Geolocation not supported";
			$('#getWiki').unbind('click');

		}
	});
	// create the geonames namespace for calling the API
	var geonames = {};
	geonames.baseURL = "http://ws.geonames.org/";
	geonames.method = "findNearbyWikipediaJSON";
	//findNearByWeatherJSON

	geonames.search = function(lat, lng) {

		// get the data in JSON format from Geonames
		$.getJSON(geonames.baseURL + geonames.method + '?formatted=true&lat=' + lat + '&lng=' + lng + '&style=full&radius=10&maxRows=25&username=ryanstewart', function(data) {

			// Loop through each item in the result and add it to the DOM
			$.each(data.geonames, function() {
				$('<li></li>').hide().append('<a href="http://' + this.wikipediaUrl + '"><h2>' + this.title + '</h2></a><br /><p>' + this.summary + '</p><span class="ui-li-aside"><h5>' + this.distance + ' (km)</h5></span>').appendTo('#wikiList').show();
			});
			// Once the data is added to the DOM, make the transition
			$.mobile.changePage('#wikilinks', "slide", false, true);

			// refresh the list to make sure the theme applies properly
			$('#wikiList').listview('refresh');

		});
	};
	// Success function for Geolocation call

	function onSuccess(positionen) {
		geonames.search(positionen.coords.latitude, positionen.coords.longitude);
	}

	// Error function for Geolocation call
	function onError(msg) {
		geonames.search(47.667622, -122.384946);
	}

});
